
public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
        
    
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    
    Room death;
    private void createRooms()
    {
        Room outside, jisichang, huolu, gouhuo, dashuku, shenlin,siwang,baoxiang;
        
        outside = new Room("无主墓地");
        jisichang = new Room("传火祭祀场", new Thing("流放者大刀", 50));
        huolu = new Room("原初火炉", new Thing("薪王们的化身",-90));
        gouhuo = new Room("篝火", new Thing("原素瓶", 20));
        dashuku = new Room("大书库", new Thing("咒术《内在潜力》", 60));
        shenlin = new Room("磔罚森林",new Thing("亡命骑士套装", 50));
        siwang = new Room("法兰要塞");
        baoxiang = new Room("隐藏房间",new Thing("宝箱",20));
        death=siwang;
        
        outside.setExit("east", gouhuo);
        outside.setExit("south", jisichang);
        
        gouhuo.setExit("west", outside);
        gouhuo.setExit("up", dashuku);
        gouhuo.setExit("down",baoxiang);
        
        jisichang.setExit("east", shenlin);
        jisichang.setExit("south", huolu);
        jisichang.setExit("nouth",outside);
        
        shenlin.setExit("up", siwang);
        shenlin.setExit("west", jisichang);
        
        baoxiang.setExit("up", gouhuo);
        
        dashuku.setExit("down", gouhuo);
        
        huolu.setExit("up",jisichang);
        currentRoom = outside;
    }
    
    
    boolean pass = false;
    public void play() 
    {            
        printWelcome();
                
        boolean finished = false;
        while (! finished || ! pass) {
            Command command = parser.getCommand();
            finished = processCommand(command);
            player.over();
        }
        System.out.println("Thank you for playing.  Good bye.");
    }
    
    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("传承之火即将熄灭，钟声响彻四周，");
        System.out.println("进而唤醒了棺木中的古老薪王，");
        System.out.println("不过啊，王者一定会舍弃王位吧。");
        System.out.println("你是无火，成不了薪，且被诅咒的不死人，");
        System.out.println("但正因如此，灰烬才是如此渴望余火吧。");
        System.out.println();
        System.out.println("你从石棺中站起，根据钟声的指引，");
         System.out.println("你将要去击败自私的薪王，传承那炽烈的初火。");
        System.out.println();
        System.out.println("需要帮助请输入‘help’");
        System.out.println();
        System.out.println("你现在位于" + currentRoom.getDescription());
        currentRoom.printExits();
    }
    
    
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不能理解你的意思...");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
            case HELP:
              printHelp();
              break;
            case GO:
              goRoom(command);
              player.step();
              break;
            case QUIT:
              wantToQuit = quit(command);
              break;
            case LOOK:
              lookThing();
              break;
            case PICK:
              pickThing();
              break;
            case CHECK:
              checkThing();
              break;
            case USE:
              useThing(command);
              break;
        }
        
        return wantToQuit;
    }
    
    
    private void over(){
        if(currentRoom==death){
          System.out.println("法兰要塞的毒气进入了你的身体，你已死亡，游戏结束。");
          System.exit(1);
        }
    }
    private void printHelp() 
    {
        System.out.println("你可以使用的命令有:");
        System.out.println(CommandWords.allCommandWords());
    }
    
    
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            System.out.println("Go where?");
            return;
        }
        

        String direction = command.getSecondWord();

        Room nextRoom = currentRoom.goRoom(direction);
        
        if (nextRoom == null) {
            System.out.println("这里没有路!");
        }
        else {
            currentRoom = nextRoom;    
            System.out.println("你在" + currentRoom.getDescription());
            currentRoom.printExits();
            over();
            if(currentRoom.getDescription() == "原初火炉") {
                pass = true;
                System.out.println("灰烬战胜了从从阻碍，击败了薪王化身");
                System.out.println("恭喜你已经完成游戏");
            }
            
            else if(currentRoom.getDescription() == "法兰要塞") {
                pass = true;
                System.out.println("法兰要塞的毒气进入了你的身体，你已死亡");
            }
            
            else if(currentRoom.getDescription() == "隐藏房间") {
                
                System.out.println("你进入隐藏房间，有额外奖励");
            }
        }
    }
    

    
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else {
            return true;
        }
    }
    
    private void lookThing()
    {
        System.out.println(currentRoom.getThing());
    }
    
    private void pickThing(){
        if(currentRoom.thing==null){
            String c = "这里什么都没有";
            System.out.println(c);
        }else{
            player.pick(currentRoom.getThings());
            currentRoom.removeThing();
        }
    }
    
    private void checkThing(){
        if (player.bag.isEmpty()){
           System.out.println("你未拾取到东西");
        }else{
           for(String key : player.bag.keySet()){
               System.out.println(key);
            }
        }
    }
    
    private void useThing(Command command){
        if(!command.hasSecondWord()){
           System.out.println("使用什么");
           return;
        }
        String food = command.getSecondWord();
        player.use(food);
    }
    
  
}
    